package com.diankangwo.controller;


import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.PhoneAndCodeDAO;
import com.diankangwo.domain.PhoneAndCode;
import com.diankangwo.util.SmsDemo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**

 * 验证码记录控制器

 *

 * @author butteflyhd

 *

 */
@Controller
@Api(description = "验证码记录")
public class PhoneAndCodeController {

    @Autowired
    private PhoneAndCodeDAO phoneAndCodeDAO;
    /**

     * 查询指定条数的验证码信息

     */
    @ResponseBody
    @RequestMapping(value = "/getcode", method = RequestMethod.GET)
    public String getcode(Integer count)
    {
        String code ="2";
        String msg = "处理异常！";
        List<PhoneAndCode> list = null;
        if(count == 0 || count == null){
            msg = "count参数异常！";
        }else{
            list = phoneAndCodeDAO.findOrderBy(10);
            code = "1";
            if(list == null || list.size() == 0 ){
                msg = "未查询到数据！";
            }else{
                msg = "查询数据成功！";
            }
        }
        return resultMsg.mapToJson(code, msg, list);
    }

    /**
     * 统一获取验证码接口
     * @param purpose
     * @param tel
     * @return
     */
    @ApiOperation(value = "统一获取验证码接口", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "purpose", value = "验证码用途", required = true, dataType = "String"),
            @ApiImplicitParam(name = "tel", value = "手机号码", required = true, dataType = "String")
    })
    @ResponseBody
    @RequestMapping(value = "/msg_code", method = RequestMethod.GET)
    public String msg_code(String purpose, String tel,HttpServletRequest req) {
        int sendcode = SmsDemo.getRandNum();
        String code = "2";
        String msg = "处理异常！";
        try {
            SendSmsResponse sendSmsResponse = SmsDemo.sendSms(tel, sendcode);
            if (sendSmsResponse.getCode().toString().equals("OK")) {
                //保存发送记录
                PhoneAndCode pac = new PhoneAndCode();
                pac.setCode(sendcode + "");
                pac.setPhone(tel);
                pac.setTime(new Date());
                pac.setType(purpose);
                pac.setMsg(sendSmsResponse.getMessage());
                pac.setStute(sendSmsResponse.getCode());
                pac.setRequestId(sendSmsResponse.getRequestId());
                phoneAndCodeDAO.saveOrUpdate(pac);
                req.getSession().setAttribute(tel, sendcode);
                System.out.println(req.getSession().getId());
                code = "1";
                msg = "发送成功！";
            } else {
                msg = "短信发送异常！";
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return resultMsg.mapToJson(code, msg, "");
    }
}
